package com.mystore.dao.impl;

import com.mystore.dao.BaseDao;
import com.mystore.dao.UserDao;
import com.mystore.entity.User;
import com.mystore.exception.UserException;
import com.mystore.vo.UserVO;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl implements UserDao {
    private BaseDao baseDao;

    public UserDaoImpl() {
        this.baseDao = new BaseDaoImpl();
    }

    /**
     * 通过用户名查找用户
     * @param username
     * @param connection
     * @return
     */
    public Object findByUsername(String username, Connection connection) throws UserException{
        String sql = "select id,username,password from user where username=?";
        List<Object> list = new ArrayList<>();
        list.add(username);
        User user =null;
        try {
           user = (User)baseDao.findSingleResult(sql,list,User.class,connection);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return user;

    }

    @Override
    public Boolean save(UserVO userVO, Connection connection) throws SQLException {
        String sql = "insert into user(username,password) values(?,?)";
        List<Object> list = new ArrayList<>();
        list.add(userVO.getUsername());
        list.add(userVO.getPassword());
        Boolean isSave = false;

            isSave =  baseDao.updateByPreparedStatement(sql,list,connection);


        return isSave;
    }
}
